package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.CommSysCode;
import com.tuanzi.loan.business.service.family.LoanAdvertService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.LoanAdvertSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanAdvertSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.family.LoanAdvertLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.primefaces.event.CellEditEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.LOAN_ADVERT_STATUS;

@Data
@Debug
@Slf4j
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanAdvertBean extends JSFListingSessionBean<LoanAdvertSearchRequest, LoanAdvertSearchResponse> implements Serializable {

    @Autowired
    private LoanAdvertService loanAdvertService;

    private List<CommSysCode> commSysCodes;

    private List<String> excludeProductIds; //批量剔除产品ID

    public void onReset() {
        initLazyDataModel();
    }

    public String onAdd() {
        return redirect("/views/family/manage/loan-advert-info.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/family/manage/loan-advert-info.xhtml?id=" + id);
    }

    public void onDelete(Long id){
        loanAdvertService.logicDelete(id);
        addSuccessfullyMessage();
    }

    @Override
    protected String page() {
        return "/views/family/manage/loan-advert.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new LoanAdvertSearchRequest();
        lazyDataModel = new LoanAdvertLazyDataModel($, session, loanAdvertService);
        commSysCodes=loanAdvertService.findAdvert();
    }
    public void changeStatus(Integer id){
        loanAdvertService.changeStatus(id);
        addSuccessfullyMessage();
    }
    public void onCellEdit(CellEditEvent event){
        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();
        if(newValue != null && !newValue.equals(oldValue)) {
            Integer position=Integer.valueOf(newValue.toString());
            Integer id=Integer.valueOf(lazyDataModel.getRowId(event.getRowIndex()).getId().toString());
            loanAdvertService.updatePosition(position,id);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Position Changed", "oldPosition: " + oldValue + ", NewPosition:" + newValue);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    /**
     * 批量剔除产品
     */
    public void onBatchEliminateProduct(){

        if(CollectionUtils.isEmpty(selectedRecords)){
            addErrorMessage("请选择记录");
            return;
        }

        if(CollectionUtils.isEmpty(excludeProductIds)){
            addErrorMessage("请选择剔除的产品");
            return;
        }

        try {

            List<Long> ids = Lists.newArrayList();
            for(LoanAdvertSearchResponse response: selectedRecords){
                ids.add((Long) response.getId());
            }
            loanAdvertService.batchEliminateProduct(ids, excludeProductIds);
            addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("批量剔除产品失败！\n"+e.getMessage());
        }

    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(LOAN_ADVERT_STATUS);
    }
}
